package com.wswy.wzcx.utils;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetDialog;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SizeUtils;
import com.wswy.wzcx.R;


public class BaseChooseDialog extends BottomSheetDialog implements View.OnClickListener {

  public BaseChooseDialog(@NonNull Context context, String... items) {
    super(context);

    NestedScrollView scrollView = new NestedScrollView(context);
    scrollView.setOverScrollMode(NestedScrollView.OVER_SCROLL_NEVER);
    scrollView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    LinearLayoutCompat container = new LinearLayoutCompat(context);
    container.setOrientation(LinearLayout.VERTICAL);
    container.setShowDividers(LinearLayoutCompat.SHOW_DIVIDER_MIDDLE);
    container.setDividerDrawable(ContextCompat.getDrawable(context, R.drawable.list_divider_h));
    scrollView.addView(container, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    addItems(context, container, items);


    setContentView(scrollView);
  }


  private void addItems(Context context, LinearLayoutCompat container, String... items) {
    int size = items.length;
    for (int i = 0; i < size; i++) {
      TextView textView = (TextView) LayoutInflater.from(context).inflate(R.layout.dialog_text_item, container, false);
      textView.setText(items[i]);
      textView.setTag(i);
      textView.setOnClickListener(this);
      container.addView(textView, ViewGroup.LayoutParams.MATCH_PARENT, SizeUtils.dp2px(48));

    }
  }

  private OnItemClickListener mOnItemClickListener;

  public BaseChooseDialog setOnItemClickListener(OnItemClickListener onItemClickListener) {
    this.mOnItemClickListener = onItemClickListener;
    return this;
  }

  public interface OnItemClickListener {
    void onItemClick(int position);
  }

  @Override
  public void onClick(View v) {
    if (mOnItemClickListener != null && v.getTag() instanceof Integer) {
      dismiss();
      mOnItemClickListener.onItemClick(((Integer) v.getTag()));
    }
  }
}
